home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 2
/
Atari Mega Archive CD - Volume 2.iso
/
minix
/
up1510b.tgz
/
up1510b
/
src
/
test
/
test20.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-07-23
|
18KB
|
691 lines
/* POSIX test program (20). Author: Andy Tanenbaum */
/* The following POSIX calls are tested:
*
* opendir()
* readdir()
* rewinddir()
* closedir()
* chdir()
* getcwd()
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <utime.h>
#include <stdio.h>
#define DIR_NULL (DIR*) NULL
#define ITERATIONS 5
#define MAX_FD 100 /* must be large enough to cause error */
#define BUF_SIZE PATH_MAX+20
#define ERR_CODE -1 /* error return */
#define RD_BUF 200
#define MAX_ERROR 4
char str[] = {"The time has come the walrus said to talk of many things.\n"};
char str2[] = {"Of ships and shoes and sealing wax, of cabbages and kings.\n"};
char str3[] = {"Of why the sea is boiling hot and whether pigs have wings\n"};
int subtest, errct;
extern errno;
main(argc, argv)
int argc;
char *argv[];
{
int i, m=0xFFFF;
sync();
if (geteuid() == 0) {
/* Must not run as root. */
setgid(2);
setuid(2);
}
if (argc == 2) m = atoi(argv[1]);
printf("Test 20 ");
fflush(stdout);
for (i = 0; i < ITERATIONS; i++) {
if (m & 00001) test20a(); /* test for correct operation */
if (m & 00002) test20b(); /* test general error handling */
if (m & 00004) test20c(); /* test for EMFILE error */
if (m & 00010) test20d(); /* test chdir() and getcwd() */
if (m & 00020) test20e(); /* test open() */
if (m & 00040) test20f(); /* test umask(), stat(), fstat() */
if (m & 00100) test20g(); /* test link() and unlink() */
if (m & 00200) test20h(); /* test access() */
if (m & 00400) test20i(); /* test chmod() and chown() */
if (m & 01000) test20j(); /* test utime() */
}
if (errct == 0)
printf("ok\n");
else
printf(" %d errors\n", errct);
exit(0);
}
test20a()
{
/* Subtest 1. Correct operation */
int f1, f2, f3, f4, f5;
DIR *dirp;
/* Remove any residue of previous tests. */
subtest = 1;
system("rm -rf foo");
/* Create a directory foo with 5 files in it. */
mkdir("foo", 0777);
if ((f1 = creat("foo/f1", 0666)) < 0) e(1);
if ((f2 = creat("foo/f2", 0666)) < 0) e(2);
if ((f3 = creat("foo/f3", 0666)) < 0) e(3);
if ((f4 = creat("foo/f4", 0666)) < 0) e(4);
if ((f5 = creat("foo/f5", 0666)) < 0) e(5);
/* Now remove 2 files to create holes in the directory. */
if (unlink("foo/f2") < 0) e(6);
if (unlink("foo/f4") < 0) e(7);
/* Close the files. */
close(f1);
close(f2);
close(f3);
close(f4);
close(f5);
/* Open the directory. */
dirp = opendir("./foo");
if (dirp == DIR_NULL) e(6);
/* Read the 5 files from it. */
checkdir(dirp, 2);
/* Rewind dir and test again. */
rewinddir(dirp);
checkdir(dirp, 3);
/* We're done. Close the directory stream. */
if (closedir(dirp) < 0) e(7);
/* Remove dir for next time. */
system("rm -rf foo");
}
checkdir(dirp, t)
DIR *dirp; /* poinrter to directory stream */
int t; /* subtest number to use */
{
int i, f1, f2, f3, f4, f5, dot, dotdot, subt;
struct dirent *d;
char *s;
/* Save subtest number */
subt = subtest;
subtest = t;
/* Clear the counters. */
f1 = 0;
f2 = 0;
f3 = 0;
f4 = 0;
f5 = 0;
dot = 0;
dotdot = 0;
/* Read the directory. It should contain 5 entries, ".", ".." and 3
* files. */
for (i = 0; i < 5; i++) {
d = readdir(dirp);
if (d == (struct dirent *) NULL) {
e(1);
subtest = subt; /* restore subtest number */
return;
}
s = d->d_name;
if (strcmp(s, ".") == 0) dot++;
if (strcmp(s, "..") == 0) dotdot++;
if (strcmp(s, "f1") == 0) f1++;
if (strcmp(s, "f2") == 0) f2++;
if (strcmp(s, "f3") == 0) f3++;
if (strcmp(s, "f4") == 0) f4++;
if (strcmp(s, "f5") == 0) f5++;
}
/* Check results. */
d = readdir(dirp);
if (d != (struct dirent *) NULL) e(2);
if (f1 != 1 || f3 != 1 || f5 != 1) e(3);
if (f2 != 0 || f4 != 0) e(4);
if (dot != 1 || dotdot != 1) e(5);
subtest = subt;
return;
}
test20b()
{
/* Subtest 4. Test error handling. */
int fd, fd2;
DIR *dirp;
subtest = 4;
if (opendir("foo/xyz/---") != DIR_NULL) e(1);
if (errno != ENOENT) e(2);
if (mkdir("foo", 0777) < 0) e(3);
if (chmod("foo", 0) < 0) e(4);
if (opendir("foo/xyz/--") != DIR_NULL) e(5);
if (errno != EACCES) e(6);
if (chmod("foo", 0777) != 0) e(7);
if (rmdir("foo") != 0) e(8);
if ((fd = creat("abc", 0666)) < 0) e(9);
if (close(fd) < 0) e(10);
if (opendir("abc/xyz") != DIR_NULL) e(11);
if (errno != ENOTDIR) e(12);
if ((dirp = opendir(".")) == DIR_NULL) e(13);
if (closedir(dirp) != 0) e(14);
if (closedir(dirp) >= 0) e(15);
if (readdir(dirp) != (struct dirent *) NULL) e(16);
if (errno != EBADF) e(17);
if (readdir(dirp - 2) != (struct dirent *) NULL) e(18);
if (errno != EBADF) e(19);
if (unlink("abc") != 0) e(20);
}
test20c()
{
/* Subtest 5. See what happens if we open too many directory streams. */
int i, j;
DIR *dirp[MAX_FD];
subtest = 5;
for (i = 0; i < MAX_FD; i++) {
dirp[i] = opendir(".");
if (dirp[i] == (DIR *) NULL) {
/* We have hit the limit. */
if (errno != EMFILE && errno != ENOMEM) e(1);
for (j = 0; j < i; j++) {
if (closedir(dirp[j]) != 0) e(2); /* close */
}
return;
}
}
/* Control should never come here. This is an error. */
e(3);
for (i = 0; i < MAX_FD; i++) closedir(dirp[i]); /* don't check */
}
test20d()
{
/* Test chdir and getcwd(). */
int i, fd;
char *s;
char base[BUF_SIZE], buf2[BUF_SIZE], tmp[BUF_SIZE];
subtest = 6;
if (getcwd(base, BUF_SIZE) == (char *) NULL) e(1); /* get test dir's path */
if (system("rm -rf Dir") != 0) e(2); /* remove residue of previous test */
if (mkdir("Dir", 0777) < 0) e(3); /* create directory called "Dir" */
/* Change to Dir and verify that it worked. */
if (chdir("Dir") < 0) e(4); /* go to Dir */
s = getcwd(buf2, BUF_SIZE); /* get full path of Dir */
if (s == (char *) NULL) e(5); /* check for error return */
if (s != buf2) e(6); /* if successful, first arg is returned */
strcpy(tmp, base); /* concatenate base name and "/Dir" */
strcat(tmp, "/");
strcat(tmp, "Dir");
if (strcmp(tmp, s) != 0) e(7);
/* Change to ".." and verify that it worked. */
if (chdir("..") < 0) e(8);
if (getcwd(buf2, BUF_SIZE) != buf2) e(9);
if (strcmp(buf2, base) != 0) e(10);
/* Now make calls that do nothing, but do it in a strange way. */
if (chdir("Dir/..") < 0) e(11);
if (getcwd(buf2, BUF_SIZE) != buf2) e(12);
if (strcmp(buf2, base) != 0) e(13);
if (chdir("Dir/../Dir/..") < 0) e(14);
if (getcwd(buf2, BUF_SIZE) != buf2) e(15);
if (strcmp(buf2, base) != 0) e(16);
if (chdir("Dir/../Dir/../Dir/../Dir/../Dir/../Dir/../Dir/..") < 0) e(17);
if (getcwd(buf2, BUF_SIZE) != buf2) e(18);
if (strcmp(buf2, base) != 0) e(19);
/* Make Dir unreadable and unsearchable. Check error message. */
if (chmod("Dir", 0) < 0) e(20);
if (chdir("Dir") >= 0) e(21);
if (errno != EACCES) e(22);
/* Check error message for bad path. */
if (chmod("Dir", 0777) < 0) e(23);
if (chdir("Dir/x/y") != ERR_CODE) e(24);
if (errno != ENOENT) e(25);
if ( (fd=creat("Dir/x", 0777)) < 0) e(26);
if (close(fd) != 0) e(27);
if (chdir("Dir/x/y") != ERR_CODE) e(28);
if (errno != ENOTDIR) e(29);
/* Check empty string. */
if (chdir("") != ERR_CODE) e(30);
if (errno != ENOENT) e(31);
/* Remove the directory. */
if (unlink("Dir/x") != 0) e(32);
if (system("rmdir Dir") != 0) e(33);
}
test20e()
{
/* Test open. */
int fd, bytes, bytes2, n;
char buf[RD_BUF];
subtest = 7;
unlink("T20"); /* get rid of it in case it exists */
/* Create a test file. */
bytes = strlen(str);
bytes2 = strlen(str2);
if ((fd = creat("T20", 0777)) < 0) e(1);
if (write(fd, str, bytes) != bytes) e(2); /* T20 now has 'bytes' bytes */
if (close(fd) != 0) e(3);
/* Test opening a file with O_RDONLY. */
if ((fd = open("T20", O_RDONLY)) < 0) e(4);
buf[0] = '\0';
if (read(fd, buf, RD_BUF) != bytes) e(5);
if (strncmp(buf, str, bytes) != 0) e(6);
if (close(fd) < 0) e(7);
/* Test the same thing, only with O_RDWR now. */
if ((fd = open("T20", O_RDWR)) < 0) e(8);
buf[0] = '\0';
if (read(fd,